package com.xsh.gls.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xsh.gls.pojo.*;
import com.xsh.gls.service.BookingService;
import com.xsh.gls.service.CompanyService;
import com.xsh.gls.service.OrderService;
import com.xsh.gls.service.YconService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author shkstart
 * @date 2019/1/24 - 9:35
 */
@RestController
@RequestMapping("/booking")
public class BookingController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private CompanyService companyService;
    @Autowired
    private BookingService bookingService;
    @Autowired
    private YconService yconService;


    /*查询订舱,将其分页显示*/
    @RequestMapping("/showbooking")
    public Object showbooking(@RequestParam(required = false)Integer shippingType,@RequestParam(required = false)Integer spelno, @RequestParam(required = false)String orderNumber, @RequestParam(required = false)String reqNo, @RequestParam(required = false)Integer sono, @RequestParam(required = false)String pageNum){
        System.out.println("传入的订舱查询条件为:"+spelno+"\t"+orderNumber+"\t"+reqNo+"\t"+sono+"\t"+shippingType);

        List<Order> orderList = null;
        Integer _pageNum=1;
        if(pageNum!=null&&!pageNum.equals("")){
            _pageNum=Integer.parseInt(pageNum);
        }
        PageHelper.startPage(_pageNum,5,"a.CREATETIME DESC");
        orderList = orderService.getByAll(spelno, orderNumber, reqNo, sono,shippingType);

        PageInfo<Order> page = new PageInfo<Order>(orderList);
        return page;

    }




    /*查询订单,跳转到申请海运订舱*/
    @RequestMapping("/showAddBooking")
    public Object showAddBooking(@RequestParam(required = false)String orderNumber) {
        System.out.println("订单号\t\t"+orderNumber);
        Order order = orderService.getByOrderNo(orderNumber);
        System.out.println("订单类型\t\t"+order.getShippingType()+"\t"+order.getId());
        List<Company> companyList = null;
        companyList = companyService.selectByType(order.getShippingType());
        System.out.println(companyList.get(0).getcName()+"\t"+companyList.get(0).getId());
        return companyList;
    }


    /*查询订单,申请海运订舱*/
    @RequestMapping("/addBooking")
    public Transfer addBooking(@RequestParam(required = false)String orderNumber,@RequestParam(required = false)Integer bookingno,@RequestParam(required = false)Integer shipinno,@RequestParam(required = false)String bookingcarrier,
                               @RequestParam(required = false)String receiver,@RequestParam(required = false)String notifyparty,
                               @RequestParam(required = false)String loadingport, @RequestParam(required = false)String dischargeport,
                               @RequestParam(required = false)String receivingplace, @RequestParam(required = false)Integer spelno,
                               @RequestParam(required = false)String remarks) {
        Transfer transfer = new Transfer();
        if (bookingService.selectByPrimaryKey(bookingno)!=null){
            transfer.setMessage("no");
        }else {
            Booking booking = new Booking();
            booking.setSpelno(spelno);
            booking.setBookingcarrier(bookingcarrier);
            booking.setCreatetime(new Date());
            booking.setDischargeport(dischargeport);
            booking.setBookingno(bookingno);
            booking.setLoadingport(loadingport);
            booking.setNotifyparty(notifyparty);
            booking.setReceiver(receiver);
            booking.setReceivingplace(receivingplace);
            booking.setRemarks(remarks);
            booking.setShipinno(shipinno);
            if (bookingService.insertSelective(booking)){
                Order order = orderService.getByOrderNo(orderNumber);
                order.setOrderNumber(orderNumber);
                order.setBookingid(bookingno);
                order.setStatusid(5);
                if (orderService.updateByorderNumber(order)){
                    Company company = companyService.selectAll(booking.getShipinno());
                    if (company.getcType()==1){
                        transfer.setMessage("toShip");
                    }else {
                        transfer.setMessage("toAir");
                    }
                }else {
                    transfer.setMessage("no");
                }
            }
        }
        return transfer;
    }


    /*反馈订舱,生成预排柜*/
    @RequestMapping("/addYcon")
    public Transfer addYcon(@RequestParam(required = false)String orderNumber,@RequestParam(required = false)String bookingReqNo,@RequestParam(required = false)Integer yconId,@RequestParam(required = false)Integer sono,
                               @RequestParam(required = false)String shipsname,@RequestParam(required = false)String shipscount,
                               @RequestParam(required = false)Integer ycontype, @RequestParam(required = false)Integer yconamount,
                               @RequestParam(required = false)Integer yconsize, @RequestParam(required = false)String etds,
                               @RequestParam(required = false)String etas,@RequestParam(required = false)String closingTimes) {
        Transfer transfer = new Transfer();
        System.out.println("预排柜信息为:"+orderNumber+"\t"+etds+"\t"+closingTimes);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date etd= null;
        Date eta= null;
        Date closingTime= null;
        try {
             etd = sdf.parse(etds);
             eta = sdf.parse(etas);
             closingTime = sdf.parse(closingTimes);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (yconService.getByYconId(yconId)!=null){
            transfer.setMessage("no");
        }else {
            Ycon ycon = new Ycon();
            ycon.setBookingReqNo(bookingReqNo);
            ycon.setYconId(yconId);
            ycon.setSono(sono);
            ycon.setShipsname(shipsname);
            ycon.setShipscount(shipscount);
            ycon.setEtd(etd);
            ycon.setEta(eta);
            ycon.setYcontype(ycontype);
            ycon.setYconamount(yconamount);
            ycon.setYconsize(yconsize);
            ycon.setClosingTime(closingTime);
            Integer yMoney = ycontype*yconamount*yconsize;
            ycon.setyMoney(yMoney);
            if (yconService.insertSelective(ycon)){
                Order order = orderService.getByOrderNo(orderNumber);
                Booking booking = bookingService.selectByPrimaryKey(order.getBookingid());
                if (booking!=null){
                    booking.setYconno(yconId);
                    order.setOrderNumber(orderNumber);
                    order.setBookingid(booking.getBookingno());
                    order.setStatusid(18);
                    if (bookingService.updateByPrimaryKeySelective(booking)){
                        if (orderService.updateByorderNumber(order)){
                            System.out.println("合作公司ID为\t\t\t"+booking.getShipinno());
                            Company company = companyService.selectAll(booking.getShipinno());
                            if (company.getcType()==1){
                                transfer.setMessage("toShip");
                            }else {
                                transfer.setMessage("toAir");
                            }

                        }
                    }
                }else {
                    transfer.setMessage("no");
                }
            }
        }
        return transfer;
    }

    /*订舱作废*/
    @RequestMapping("/deleteBooking")
    public Transfer deleteBooking(@RequestParam(required = false)String orderNumber) {
        Transfer transfer = new Transfer();
        Order order = orderService.getByOrderNo(orderNumber);
        if (order.getBookingid()==null){
            transfer.setMessage("no");
        }else {
            System.out.println("订舱申请号为\t\t"+order.getBookingid());
            Booking booking = bookingService.selectByPrimaryKey(order.getBookingid());
            if (booking.getYconno()==null){
                if (bookingService.deleteByBookingno(booking.getBookingno())){
                    transfer.setMessage("yes");
                }else {
                    transfer.setMessage("no");
                }
            }else {
                if (yconService.deleteByYconid(booking.getYconno())){
                    System.out.println("订舱申请号\t\t"+booking.getBookingno());
                    if (bookingService.deleteByBookingno(order.getBookingid())){
                        System.out.println(order.getBookingid()+"\t"+order.getId()+"\t"+order.getStatusid());
                        order.setStatusid(33);
                        if (orderService.updateByBookingno(order)){
                            transfer.setMessage("yes");
                        }
                    }
                }
            }
        }
        return transfer;
    }


    /*跳转到查询订舱，查询订舱详细信息*/
    @RequestMapping("/showCheckBooking")
    public Object showCheckBooking(@RequestParam(required = false)String orderNumber) {
        System.out.println("订单号\t\t"+orderNumber);
        Order order = orderService.getByOrderNo(orderNumber);
        System.out.println("订舱申请号\t\t"+order.getBookingid());
        Booking booking = bookingService.selectByPrimaryKey(order.getBookingid());
        return booking;
    }



    /*跳转到查询订舱，查询订舱详细信息*/
    @RequestMapping("/showCheckYcon")
    public Object showCheckYcon(@RequestParam(required = false)Integer yconId) {
        System.out.println("预排柜号\t\t"+yconId);
        Ycon ycon = yconService.getByYconId(yconId);
        return ycon;
    }


    /*订舱反馈页面跳转回去*/
    @RequestMapping("/checkOrderType")
    public Transfer checkOrderType(@RequestParam(required = false)String orderNumber) {
        Transfer transfer = new Transfer();

        System.out.println("订单号\t\t"+orderNumber);
        Order order = orderService.getByOrderNo(orderNumber);
        if (order==null){
            transfer.setMessage("no");
        }else {
            if (order.getShippingType()==1){
                transfer.setMessage("toShip");
            }else {
                transfer.setMessage("toAir");
            }
        }
        return transfer;
    }
}
